package com.snts.synchronization.core;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UUID {

	private String value;
	private static final Pattern uuidPattern = Pattern.compile("[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}");

	public static UUID newUUID(){
		return new UUID(java.util.UUID.randomUUID().toString());
	}
	
	public UUID(String value) {
		super();
		if(value == null) { 
			throw new IllegalArgumentException("\"value\" cannot be null"); 
		}
		if(! isValidUUID(value)){
			throw new IllegalArgumentException("\"" + value + "\" is invalid uuid");
		}
		this.value = value;
	}

	public static boolean isValidUUID(String value) {
		return value != null && uuidPattern.matcher(value).matches(); 
	}

	@Override
	public int hashCode() {
		return value.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if(obj instanceof UUID){
			UUID uuid = (UUID) obj;
			for(int i = 0; i < value.length(); i ++){
				if(value.charAt(i) != uuid.value.charAt(i)){
					return false;
				}
			}
			return true;
		} else {
			return false;
		}
	}
	
	@Override
	public String toString() {
		return value;
	}

	public static void main(String[] args){
		/*System.out.println("0714b54e-b9df-11e1-8399-efdd549c38b6".length());
		Matcher matcher = uuidPattern.matcher("1edb5898-c5b7-11e1-b086-ab61c0ffe581");
		if(matcher.matches()){
			System.out.println("Matches");
		}*/
		System.out.println(java.util.UUID.randomUUID().toString());
		System.out.println(UUID.newUUID().toString());
	}
	
	
	
}
